# -*- shell-script -*-

# 99vpd_file - Backward compatibilty for linux,vpd files.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2004

# Maintained by  Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 99vpd_file,v 1.1 2006/04/11 18:38:28 emunson Exp $

# This condition is too weak, but it will do for now...  :-)
[ -f "${db_bus_dt_dir}/linux,vpd" ] || return 0

######################################################################

vpd_subdirs_list_hook ()
{
    local node="$1"

    local vpd_dir line f n

    vpd_subdirs=""

    f="${node}/linux,vpd"

    [ -f "$f" ] || return

    n=0
    while read line ; do
	case "$line" in
	    ?FC*)
                vpd_dir_set_hook "$node" $n
		[ -n "$vpd_subdirs" ] && vpd_subdirs="${vpd_subdirs} "
		vpd_subdirs="${vpd_subdirs}${vpd_dir}"
                n=$(($n + 1))
        esac
    done <"$f"
}

vpd_fields_list_hook ()
{
    local vpd_dir="$1"

    local f num line n

    vpd_fields=""

    f="${vpd_dir%/*}"
    num="${vpd_dir##*/}"

    {
	n=0
	while read line ; do
	    case "$line" in
		?FC*)
		    if [ $n -eq $num ] ; then
			break
		    else
			n=$(($n + 1))
		    fi
	    esac
	done

	if [ $n -eq $num ] ; then
	    while read line ; do
		case "$line" in
		    (?FC*) return ;;
		    *)
			[ -n "$vpd_fields" ] && vpd_fields="${vpd_fields} "
			vpd_fields="${vpd_fields}${line:1:2}"
			;;
		esac
	    done
	fi
    } <"$f"
}

# For non-unique field names, this interface can be used in 2 ways.
# The 3 digit sequence number for fields (after the first, numbered 0,
# but omitted) can be appended to the field name (as in ZZ.002), or
# the sequence number can be provided as an optional 3rd argument.
vpd_field_get ()
{
    local vpd_dir="$1"
    local field="$2"
    local seq="$3"

    local f s num line n

    vpd_fields=""

    f="${vpd_dir%/*}"
    num="${vpd_dir##*/}"
    [ -n "$seq" ] || seq=0

    {
	n=0
	while read line ; do
	    case "$line" in
		?FC*)
		    if [ $n -eq $num ] ; then
			break
		    else
			n=$(($n + 1))
		    fi
	    esac
	done

	if [ $n -eq $num ] ; then
	    s=0
	    while read line ; do
		case "$line" in
		    (?FC*) return ;;
		    ?${field}*)
                        if [ $s -eq $seq ] ; then
			    echo "${line:4}"
			    return
			else
			    seq=$(($seq + 1))
			fi
			;;
		esac
	    done
	fi
    } <"$f"
}
